-- mechanical_damage
-- create by xinj
-- 机械装置单体伤害

return {
    apply = function(source, target, skillId, round, para, extra)
        -- 威力
        local force = para[1];
        local level = 0;

        if type(force) ~= "number" then
            return;
        end

        -- ------------------------- 基础伤害 ----------------------
        -- 机械师专属加强效果
        local prop = PropM.combine(source, "mechanics", 1);
        level = PropM.apply(prop, level);
        if prop[3] > 0 then
            -- 触发特技发动事件
            EventMgr.fire(event.SPECIAL_SKILL_TRIGGER, { propId = prop[1] });
        end

        if level >= 1 then
            force = force + 50;
        end
        if level >= 3 then
            force = force + 50;
        end
        if level >= 5 then
            force = force + 50;
        end

        local damage = force;

        local addon = 0;
        local args  = {["value"] = damage, ["class_id"] = extra.propertyId,};

        -- 概率造成额外的固定伤害
        local props = PropM.fetchProps(source, "prob_fixed_damage");
        for _, prop in ipairs(props) do
            addon = addon + PropM.trigger(source, prop[1], prop[2], args);
        end

        damage = damage + addon;

        -- ------------------------- 百分比加成 ----------------------
        -- 无生命敌人伤害加成
        if level >= 2 and not FormulaM.invoke("IS_LIVING_MONSTER", target) then
            damage = math.modf(damage * 15 / 10);
        end

        -- 道具伤害加成
        prop = PropM.combine(source, "property_damage", 1);
        damage = PropM.apply(prop, damage);

        -- 概率造成额外的百分比伤害
        props = PropM.fetchProps(source, "prob_per_damage");
        for _, prop in ipairs(props) do
            args.value = damage;
            damage = damage + PropM.trigger(source, prop[1], prop[2], args);
        end

        -- 道具修正伤害
        local classId = extra["propertyId"];
        local fixDamage = damage;
        if type(classId) == "number" then
            fixDamage = PropertyM.fixPropertyDamage(source, target, classId, fixDamage);
        end

        -- 增加命中动作
        SkillM.getSequence(target):hit(source, target, skillId);

        -- 伤害
        CombatM.receiveDamage(source, target, fixDamage, skillId, extra);

        -- 额外燃烧效果
        if level >= 4 then
            local propId = PropM.getPropId("burn");
            local burnDamage = math.max(1, math.modf(damage * 2 / 10));
            local endRound = CombatM.getRound() + 3;
            local arr = {propId, 1, burnDamage, endRound};
            local condition = {
                ["prop"] = arr,
                ["end_round"] = endRound,
            };

            CombatStatusM.applyStatus(target, "burn", condition);
        end

        -- 如果受攻击对象是怪物，抛出投掷类攻击事件
        if target.type == OBJECT_TYPE_MONSTER then
            -- 播放特效所需的参数
            local effectInfo = {
                ["move_type"] = para[2],
                ["effect_id"] = para[3],
                ["tail_id"] = para[4] ~= 0 and para[4] or nil,
                ["offset"] = cc.p(para[5], para[6]),
                ["pos"] = target:getOwner();
                ["icon_id"] = 7213;
            };

            -- 抛出投掷类攻击事件
            EventMgr.fire(event.SKILL_ATTACK, { ["effect_info"] = effectInfo, });
        end

        -- 作用目标
        return { target, };
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return {tonumber(arr[1]), arr[2], tonumber(arr[3]), tonumber(arr[4]), tonumber(arr[5]), tonumber(arr[6])};
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 无需技能描述
        return desc;
    end,
};
